-- This addon shows timers started by DeadlyBossMods "Pizzatimer"-feature or 
-- started by /dbm pull <seconds> as BigWigs local timerbars

-- tested in combo with DBM 4.26 and BigWigs 3.0 svn-checkout r6349
-- does not work with DBM 3.x anymore
local BigWigs = _G.BigWigs
if not BigWigs then return end

local mod = BigWigs:NewPlugin("PizzaBar", "AceEvent-3.0", "AceSerializer-3.0")
if not mod then return end

local function debug(arg)
--[===[@debug@
	ChatFrame4:AddMessage("BigWigsPB: " .. arg)
--@end-debug@]===]
end

local MAX_WAIT = 7
local last_query_time

local function output(arg)
	BigWigs:Print("PizzaBar: " .. arg) 
end

function mod:OnPluginEnable()
	mod:RegisterEvent("CHAT_MSG_ADDON")
end

local function getOption(key) return mod.db.profile[key] end
local function setOption(key, value) mod.db.profile[key] = value end

local ICON = "Interface\\Icons\\INV_Misc_PocketWatch_01"

local key = 0
local function nextKey()
	key = key + 1
	return key
end

function mod:CHAT_MSG_ADDON(_, prefix, message, distributionType, sender)
	if distributionType == "RAID" and sender ~= UnitName("player") then
		debug("[all]" .. message:gsub("\t", "   "))
		if (prefix == "DBMv4-Pizza" and getOption("pizza")) or 
					(prefix == "DBMv4-Pull" and getOption("pull")) then
			local time, name = ("\t"):split(message)
			time = tonumber(time)
			-- make sure the timer is valid
			if type(name) ~= 'string' then
				return
			end
			if not time or time <= 0 then
				return
			end
			--[[
			copy of function-definition from BigWigs/Bars.lua
			function plugin:BigWigs_StartBar(message, module, key, text, time, icon)
			--]]
			self:SendMessage("BigWigs_StartBar", self, nextKey(), name, time, ICON)
		elseif prefix == "DBMv4-Ver" and getOption("version") then
			debug("DBMv4-Ver recieved")
			if message == "Hi!" then
					local vermsg = ("%s\t%s\t%s\t%s"):format(BigWigs.revision, "0.0", "0.0", GetLocale())
					SendAddonMessage("DBMv4-Ver", vermsg, "RAID")
			elseif (GetTime() - last_query_time) < MAX_WAIT then
				local revision, version, displayVersion, locale = strsplit("\t", message)
				output(("%s: %s (%s)"):format(sender, displayVersion, locale))
			end			
		elseif prefix == "DBMv4-Mod" and getOption("mod") then
			debug("Mod: " .. msg)
		elseif prefix == "DXE" then
			local success, _, time, name = self:Deserialize(message)
			if success then
				self:SendMessage("BigWigs_StartBar", self, nextKey(), name, time, ICON)
			end
		end
	end
end

mod.defaultDB = {
	pizza = true,
	pull = true,
	version = false,
	mod = false,
}

local function startRaidBar(input)
	if not mod:IsEnabled() then BigWigs:Enable() end
	if not IsRaidOfficer() then
		output("You are not allowed to start \"pizza timers\"")
		return
	end
	local time, name = (" "):split(input)
	time = tonumber(time)
	if not time or not name then return end
	SendAddonMessage("BigWigs", "BWCustomBar " .. input, "RAID")
	SendAddonMessage("DBMv4-Pizza", ("%s\t%s\t%s"):format(UnitName("player"), time, name), "RAID")
	SendAddonMessage("DXE", mod:Serialize("AlertsRaidBar", time, name), "RAID")
end

_G["SlashCmdList"]["BWPB_SHORTHAND"] = startRaidBar
_G["SLASH_BWPB_SHORTHAND1"] = "/bwpb"

mod.pluginOptions = {
	type = "group",
	name = "PizzaBar",
	desc = "Options for the PizzaBar plugin.",
	args = {
		pizza = {
			type = "toggle",
			name = "Pizza-timer",
			desc = "Show Pizza-timers",
			order = 1,
			get = function() return getOption("pizza") end,
			set = function(info, value) setOption("pizza", value) end,
		},
		pull = {
			type = "toggle",
			name = "Pull-timer",
			desc = "Show Pull-timers",
			order = 2,
			get = function() return getOption("pull") end,
			set = function(info, value) setOption("pull", value) end,
		},
		version = {
			type = "toggle",
			name = "Version-query",
			desc = "Respond to DBM-version-queries (version: 0.0, revision: BigWigs-revision)",
			order = 3,
			get = function() return getOption("version") end,
			set = function(info, value) setOption("version", value) end,
		},
		--@debug@
		mod = {
			type = "toggle",
			name = "Mod-sync-messages (debug only)",
			desc = "Show DBMv4-Mod-Messages in debug-window",
			order = 4,
			get = function() return getOption("mod") end,
			set = function(info, value) setOption("mod", value) end,
		},
		--@end-debug
		pizzatimer = {
			type = "input",
			name = "Start Pizza-Timer",
			desc = "Start Pizza-Timer, that shows in both BigWigs and DBM",
			order = 5,
			get = false,
			set = function(info, v)
					startRaidBar(v)
				end,
			usage = "<time> <bar text>",
		},
	},
}
